//
//  MethodView.swift
//  swiftStudy
//
//  Created by ext.zhangjunfei on 2021/7/8.
//

import SwiftUI

struct MethodView: View {
    
    let code_1:String
    
    init() {
        code_1 =
            """
            class MyArray{
                var array:Array<Int>

                init(patam:Int...) {
                    array = patam
                }
                
                //定义下标
                subscript(index:Int)->Int{
                    set{
                        array[index] = newValue
                    }
                    get{
                        return array[index]
                    }
                }
            }

            var myArray = MyArray(patam: 1,2,3,4,5)

            print(myArray[2])
            """
    }
    
    var body: some View {
        
        ScrollView{

            LazyVStack(alignment: .leading, spacing: 10) {
                Section(header: SectionHeaderView(title: "下标方法")) {

                    CommonTextView(content: "下标使用subscript来定义，其与普通方法类似，参数和返回值分别作为下标和通过下标所取得的值。其必须实现一个get代码块和可选的set代码块，get代码块用于使用下标取值，set代码块用于使用下标设置值。")

                    CodeView(code: code_1).padding(10)
                }
            }
        }
    }
}

struct MethodView_Previews: PreviewProvider {
    static var previews: some View {
        MethodView()
    }
}
